# Copyright 2022 Flant JSC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# on push to default branch or on tags
{!{- $ctx := dict -}!}
{!{- $ctx = coll.Merge $ctx . -}!}
{!{- $jobNames := dict -}!}
{!{- $workflowName := "Build and test for release" }!}

name: {!{ $workflowName }!}

on:
  workflow_dispatch:
    inputs:
      issue_id:
        description: 'Id of issue where label was set'
        required: false
      issue_number:
        description: 'Number of issue where label was set'
        required: false
      comment_id:
        description: 'Id of comment in issue where to put workflow run status'
        required: false

env:
{!{ tmpl.Exec "werf_envs"               | strings.Indent 2 }!}
{!{ tmpl.Exec "git_source_envs"               | strings.Indent 2 }!}

# Cancel in-progress jobs for the same tag/branch.
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  started_at:
    name: Save start timestamp
    outputs:
      started_at: ${{ steps.started_at.outputs.started_at }}
    runs-on: "ubuntu-latest"
    steps:
{!{ tmpl.Exec "started_at_output" . | strings.Indent 6 }!}

{!{ tmpl.Exec "git_info_job" $ctx | strings.Indent 2 }!}

  comment_on_start:
    name: Update issue comment
    runs-on: ubuntu-latest
    steps:
{!{ tmpl.Exec "checkout_step" $ctx | strings.Indent 6 }!}
{!{ tmpl.Exec "update_comment_on_start" "Build and test for release" | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "go_generate" "Go Generate") }!}
  go_generate:
    name: {!{ $jobNames.go_generate }!}
    needs:
      - git_info
{!{ tmpl.Exec "go_generate_template" $ctx | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.go_generate) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "workflow_render" "Render workflow") }!}
  workflow_render:
    name: {!{ $jobNames.workflow_render }!}
    needs:
      - git_info
{!{ tmpl.Exec "workflow_render_template" $ctx | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.workflow_render) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "build_fe" "Build FE") }!}
  build_fe:
    name: {!{ $jobNames.build_fe }!}
    needs:
      - git_info
      - go_generate
      - workflow_render
    env:
      WERF_ENV: "FE"
{!{ tmpl.Exec "build_template" (slice $ctx "release") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.build_fe) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "build_ee" "Build EE") }!}
  build_ee:
    name: {!{ $jobNames.build_ee }!}
    needs:
      - git_info
      - go_generate
      - workflow_render
    if: ${{ startsWith(github.ref, 'refs/tags/v') }}
    env:
      WERF_ENV: "EE"
{!{ tmpl.Exec "build_template" (slice $ctx "release") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.build_ee) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "build_se" "Build SE") }!}
  build_se:
    name: {!{ $jobNames.build_se }!}
    needs:
      - git_info
      - go_generate
      - workflow_render
    if: ${{ startsWith(github.ref, 'refs/tags/v') }}
    env:
      WERF_ENV: "SE"
{!{ tmpl.Exec "build_template" (slice $ctx "release") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.build_se) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "build_be" "Build BE") }!}
  build_be:
    name: {!{ $jobNames.build_be }!}
    needs:
      - git_info
      - go_generate
      - workflow_render
    if: ${{ startsWith(github.ref, 'refs/tags/v') }}
    env:
      WERF_ENV: "BE"
{!{ tmpl.Exec "build_template" (slice $ctx "release") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.build_be) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "build_ce" "Build CE") }!}
  build_ce:
    name: {!{ $jobNames.build_ce }!}
    needs:
      - git_info
      - go_generate
      - workflow_render
    if: ${{ startsWith(github.ref, 'refs/tags/v') }}
    env:
      WERF_ENV: "CE"
{!{ tmpl.Exec "build_template" (slice $ctx "release") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.build_ce) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "doc_web_build" "Doc web build") }!}
  doc_web_build:
    name: {!{ $jobNames.doc_web_build }!}
    # Wait for success build of modules.
    needs:
      - git_info
{!{ tmpl.Exec "web_build_template" (slice $ctx "doc" "release") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.doc_web_build) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "main_web_build" "Main web build") }!}
  main_web_build:
    name: {!{ $jobNames.main_web_build }!}
    # Wait for success build of modules.
    needs:
      - git_info
{!{ tmpl.Exec "web_build_template" (slice $ctx "main" "release") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.main_web_build) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "tests" "Tests") }!}
  tests:
    name: {!{ $jobNames.tests }!}
    needs:
      - git_info
{!{ tmpl.Exec "tests_before_build_template" (slice $ctx "unit") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.tests) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "matrix_tests" "Matrix tests") }!}
  matrix_tests:
    name: {!{ $jobNames.matrix_tests }!}
    needs:
      - git_info
{!{ tmpl.Exec "tests_before_build_template" (slice $ctx "matrix") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.matrix_tests) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "dhctl_tests" "Dhctl Tests") }!}
  dhctl_tests:
    name: {!{ $jobNames.dhctl_tests }!}
    needs:
      - git_info
      - build_fe
{!{ tmpl.Exec "tests_template" (slice $ctx "dhctl" "build_fe") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.dhctl_tests) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "golangci_lint" "GolangCI Lint") }!}
  golangci_lint:
    name: {!{ $jobNames.golangci_lint }!}
    needs:
      - git_info
      - build_fe
{!{ tmpl.Exec "tests_template" (slice $ctx "golangci_lint" "build_fe") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.golangci_lint) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "openapi_test_cases" "OpenAPI Test Cases") }!}
  openapi_test_cases:
    name: {!{ $jobNames.openapi_test_cases }!}
    needs:
      - git_info
      - build_fe
{!{ tmpl.Exec "tests_template" (slice $ctx "openapi_test_cases" "build_fe") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.openapi_test_cases) | strings.Indent 6 }!}

{!{ $jobNames = coll.Merge $jobNames (dict "web_links_test" "Web links test") }!}
  web_links_test:
    name: {!{ $jobNames.web_links_test }!}
    if: ${{ false || github.repository == 'deckhouse/deckhouse' }}
    needs:
      - git_info
      - doc_web_build
      - main_web_build
{!{ tmpl.Exec "web_links_test_template" (slice $ctx "release") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.web_links_test) | strings.Indent 6 }!}

{!{- $jobNames = coll.Merge $jobNames (dict "validators" "Validators") }!}
  validators:
    name: {!{ $jobNames.validators }!}
    needs:
      - git_info
      - build_fe
{!{ tmpl.Exec "tests_template" (slice $ctx "validators" "build_fe") | strings.Indent 4 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.validators) | strings.Indent 6 }!}

{!{/* Autodeploy site to production (SEL) env on push to the main branch. */}!}
{!{- $jobNames = coll.Merge $jobNames (dict "deploy_latest_web_site_prod_sel" "(Prod) Deploy latest site") }!}
  deploy_latest_web_site_prod_sel:
    name: {!{ $jobNames.deploy_latest_web_site_prod_sel }!}
    needs:
      - git_info
      - doc_web_build
      - main_web_build
    if: ${{ needs.git_info.outputs.ci_commit_ref_name == 'main' && github.repository == 'deckhouse/deckhouse' }}
    runs-on: [self-hosted, regular]
    steps:
{!{ tmpl.Exec "started_at_output" . | strings.Indent 6 }!}
{!{ tmpl.Exec "checkout_full_step" $ctx | strings.Indent 6}!}
{!{ tmpl.Exec "login_readonly_registry_step" $ctx | strings.Indent 6 }!}
{!{ tmpl.Exec "doc_version_template" | strings.Indent 6 }!}
{!{ tmpl.Exec "deploy_site_template" "preproduction" | strings.Indent 6 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.deploy_latest_web_site_prod_sel) | strings.Indent 6 }!}

{!{/* Autodeploy docs to production (SEL) env on push to the main branch. */}!}
{!{- $jobNames = coll.Merge $jobNames (dict "deploy_latest_web_doc_prod_sel" "(Prod) Deploy latest doc") }!}
  deploy_latest_web_doc_prod_sel:
    name: {!{ $jobNames.deploy_latest_web_doc_prod_sel }!}
    needs:
      - git_info
      - doc_web_build
      - main_web_build
    if: ${{ needs.git_info.outputs.ci_commit_ref_name == 'main' && github.repository == 'deckhouse/deckhouse' }}
    runs-on: [self-hosted, regular]
    steps:
{!{ tmpl.Exec "started_at_output" . | strings.Indent 6 }!}
{!{ tmpl.Exec "checkout_full_step" $ctx | strings.Indent 6}!}
{!{ tmpl.Exec "login_readonly_registry_step" $ctx | strings.Indent 6 }!}
{!{ tmpl.Exec "doc_version_template" | strings.Indent 6 }!}
{!{ tmpl.Exec "deploy_doc_template" "preproduction" | strings.Indent 6 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "job,one-line" $jobNames.deploy_latest_web_doc_prod_sel) | strings.Indent 6 }!}

  last_comment:
    name: Update comment on finish
    needs:
      - started_at
      - git_info
      - go_generate
      - workflow_render
      - build_fe
      - build_ee
      - build_ce
      - build_be
      - build_se
      - doc_web_build
      - main_web_build
      - deploy_latest_web_site_prod_sel
      - deploy_latest_web_doc_prod_sel
      - tests
      - matrix_tests
      - dhctl_tests
      - golangci_lint
      - openapi_test_cases
      - web_links_test
      - validators
    if: ${{ always() }}
    runs-on: ubuntu-latest
    env:
      JOB_NAMES: |
        {!{ $jobNames | toJSON }!}
    steps:
{!{ tmpl.Exec "checkout_step" $ctx | strings.Indent 6 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "workflow,final,restore-one-line" $workflowName) | strings.Indent 6 }!}
